package router

import (
	"database-platform/configs"
	"database-platform/handler"
	"database-platform/middleware"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

func Setup() *gin.Engine {
	gin.SetMode(configs.Conf.Mode)

	r := gin.New()

	// 注册两个自定义的中间件
	r.Use(middleware.Cors())
	r.Use(middleware.GinLogger(zap.L()))
	r.Use(middleware.GinRecovery(zap.L(), false))

	// 基础路由
	r.GET("/ping", handler.PingHandler)

	// 路由拆分
	dispatch(r)
	return r
}

func dispatch(e *gin.Engine) []*gin.RouterGroup {
	rg := make([]*gin.RouterGroup, 0)
	rg = append(rg, Init(e))
	rg = append(rg, InitClusterRoutes(e))
	rg = append(rg, InitInstanceRoutes(e))
	rg = append(rg, InitDatabaseRoutes(e))
	rg = append(rg, InitAccountRoutes(e))
	return rg
}
